/**
 * @filename:SysCommonBaseServiceImpl 2020年06月17日
 * @project hotel-admin  V1.0
 * Copyright(c) 2018 Zhang·JZ Co. Ltd.
 * All right reserved.
 */
package hotel.quartz.oversea.service.impl;

import hotel.base.oversea.service.impl.BaseServiceImpl;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.vo.SysCommonBaseReqVO;
import hotel.quartz.oversea.config.service.RedisService;
import hotel.quartz.oversea.mapper.SysCommonBaseMapper;
import hotel.quartz.oversea.service.SysCommonBaseService;
import hotel.quartz.oversea.vo.SysCommonBase;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;


/**
 * @Description:TODO(系统公共基础参数表服务实现)
 * @version: V1.0
 * @author: Zhang·JZ
 */
@Service
@Slf4j
public class SysCommonBaseServiceImpl extends BaseServiceImpl<SysCommonBase> implements SysCommonBaseService {
    @Autowired
    private SysCommonBaseMapper sysCommonBaseMapper;

    @Autowired
    private RedisService redisService;

    @Override
    public SysCommonBase getSysCommonBaseById(String id) {
        Example example = new Example(SysCommonBase.class);
        example.createCriteria().andCondition("id=", id);
        List<SysCommonBase> list = sysCommonBaseMapper.selectByExample(example);
        return list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public List<SysCommonBase> list(String id) {
        Example example = new Example(SysCommonBase.class);
        Example.Criteria criteria = example.createCriteria();
        if (!ObjectUtils.isEmpty(id)) {
            criteria.orLike("id like ", id + "%");
        }
        return sysCommonBaseMapper.selectByExample(example);
    }

    @Override
    public void create(SysCommonBase sysCommonBase) {
        sysCommonBaseMapper.insert(sysCommonBase);
    }

    @Override
    public void update(SysCommonBase sysCommonBase) {
        this.updateNotNull(sysCommonBase);
    }

    /**
     * <H1>获取系统公共基础参数列表</H1>
     *
     * @param sysCommonBaseReqVO 实体对象
     * @return java.util.List<hotel.admin.api.entity.SysCommonBase>
     * @author HuangT
     * @Date 2020/10/12 16:05
     */
    @Override
    public List<SysCommonBase> getList(SysCommonBaseReqVO sysCommonBaseReqVO) {
        //获取系统公共基础参数列表
        List<SysCommonBase> list = sysCommonBaseMapper.getList();
        if(ObjectUtils.isEmpty(list)||list.size()<0){
            return new ArrayList<>();
        }
        List<SysCommonBase> retList = new ArrayList<>();
        Map<SysCommonBase,List<SysCommonBase>> map=new HashMap<>();
        //组装数据格式
        Iterator<SysCommonBase> it = list.iterator();
        Map<SysCommonBase,String> hasMake=new HashMap<>();
        while (it.hasNext()) {
            SysCommonBase sysCommonBase = it.next();
            String parentId = sysCommonBase.getId();
            Iterator<SysCommonBase> itChild = list.iterator();
            while (itChild.hasNext()) {
                SysCommonBase sysCommonBaseChild = itChild.next();
                String childId = sysCommonBaseChild.getId();
                if(childId.indexOf(parentId+"_") != -1){
                    if (map.containsKey(sysCommonBase)) {
                        map.get(sysCommonBase).add(sysCommonBaseChild);
                    } else {
                        List<SysCommonBase> children = new ArrayList<>();
                        map.put(sysCommonBase, children);
                    }
                    hasMake.put(sysCommonBase,"1");
                    hasMake.put(sysCommonBaseChild,"1");
                }
            }
        }
        it = list.iterator();
        while(it.hasNext()){
            SysCommonBase sysCommonBase = it.next();
            if(!hasMake.containsKey(sysCommonBase)){
                map.put(sysCommonBase,new ArrayList<>());
            }
        }

        for(Map.Entry<SysCommonBase, List<SysCommonBase>> entry : map.entrySet()){
            SysCommonBase mapKey = entry.getKey();
            List<SysCommonBase> mapValue = entry.getValue();
            mapKey.setChildren(mapValue);
            retList.add(mapKey);
        }

        Collections.sort(retList, (a, b) -> b.getOpSort().compareTo(a.getOpSort()));
        return retList;
    }

    @Override
    public List<SysCommonBase> queryCommonBaseByGroupHead(Map map) {
        return sysCommonBaseMapper.queryCommonBaseByGroupHead(map);

    }
    /***
    * @Author: lmf
    * @Date: 2020/12/9
     * 添加基础配置
    */
    @Override
    public Result<Object> addCommonBase(SysCommonBase sysCommonBase) {
        sysCommonBase.setOpState(0);
        int flag= sysCommonBaseMapper.insert(sysCommonBase);
        if(flag>0){
            redisService.set(sysCommonBase.getId(),sysCommonBase.getOpValue());
            return new Result<>();
        }else{
            return new Result<>("添加失败");
        }
    }
    /***
     * @Author: lmf
     * @Date: 2020/12/9
     * 添加基础配置
     */
    @Override
    public Result<Object> updateCommonBase(SysCommonBase sysCommonBase) {
        Example example=new Example(SysCommonBase.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("id",sysCommonBase.getId());
        sysCommonBase.setOpState(0);
        int flag= sysCommonBaseMapper.updateByExample(sysCommonBase,example);
        if(flag>0){
            redisService.set(sysCommonBase.getId(),sysCommonBase.getOpValue());
            return new Result<>();
        }else{
            return new Result<>("更新失败");
        }
    }
    /***
    * @Author: lmf
    * @Date: 2020/12/9
     * 删除基础配置
    */
    @Override
    public Result<Object> delCommonBase(SysCommonBase sysCommonBase) {
        Example example=new Example(SysCommonBase.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("id",sysCommonBase.getId());
        sysCommonBase.setOpState(sysCommonBase.getOpState());
        int flag= sysCommonBaseMapper.updateByExample(sysCommonBase,example);
        if(flag>0){
            if(sysCommonBase.getOpState()==0){
                redisService.set(sysCommonBase.getId(),sysCommonBase.getOpValue());
            }else{
                redisService.remove(sysCommonBase.getId());
            }
            return new Result<>();
        }else{
            return new Result<>("删除失败");
        }
    }
}
